package cc.cary.warehouse.client.config;

import cc.cary.warehouse.client.annotation.RequestSigned;
import cc.cary.warehouse.untils.Beans;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.interceptor.Interceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 请求签名拦截器
 *
 * @author Cary
 * @date 2023/4/23
 */
@Component
public class RequestSignedInterceptor implements Interceptor<Object> {
    @Value("${warehouse.api-secret}")
    private String secret;

    @Override
    public boolean beforeExecute(ForestRequest request) {
        RequestSigned requestSigned = request.getMethod().getMethod().getAnnotation(RequestSigned.class);
        if (Beans.isNotNull(requestSigned)) {
            request.addHeader("Authorization", secret);
        }
        return true;
    }
}
